package com.example.demo4;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

/**
 * @author 张成
 * @version 1.0
 * @date 2022/3/15 10:53
 */
@WebServlet(name = "ExamServlet", value = "/ExamServlet")
public class ExamServlet extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response) throws ServletException, IOException {

    }

    public void doPost(HttpServletRequest request,
                       HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String one = request.getParameter("one");

        String capital = request.getParameter("capital").trim();
        int sum = 0;
        if ("C".equalsIgnoreCase(one)) {
            sum += 10;
        }
        String two = Arrays.toString(request.getParameterValues("two"));
        if ("北京".equalsIgnoreCase(capital)) {
            sum += 10;
        }
        if (!two.contains("A")) {
            if (two.contains("B")) {
                sum += 5;
            }
            if (two.contains("C")) {
                sum += 5;
            }
        }
        response.setCharacterEncoding("gb2312");
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>你的分数, " + sum + "分</h1>");
        out.println("</body>");
        out.println("</html>");

    }
}
